package com.chen.shopping_goods_service.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.shopping_common.pojo.ProductType;
import com.chen.shopping_common.result.BusException;
import com.chen.shopping_common.result.CodeEnum;
import com.chen.shopping_common.service.ProductTypeService;
import com.chen.shopping_goods_service.mapper.ProductTypeMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

@DubboService
@Transactional
public class ProductTypeServiceImpl implements ProductTypeService {

    @Autowired
    private ProductTypeMapper productTypeMapper;

    @Override
    public void addProductType(ProductType productType) {
        ProductType productParent = productTypeMapper.selectById(productType.getParentId());
        if (productParent == null){
            productType.setLevel(1);
        } else if (productParent.getLevel() < 3) {
            productType.setLevel(productParent.getLevel()+1);
        } else if (productParent.getLevel() == 3){
            throw new BusException(CodeEnum.INSERT_PRODUCTTYPE_ERROR);
        }
        productTypeMapper.insert(productType);
    }

    @Override
    public void updateProductType(ProductType productType) {
        ProductType productParent = productTypeMapper.selectById(productType.getParentId());
        if (productParent == null){
            productType.setLevel(1);
        } else if (productParent.getLevel() < 3) {
            productType.setLevel(productParent.getLevel()+1);
        } else if (productParent.getLevel() == 3){
            throw new BusException(CodeEnum.INSERT_PRODUCTTYPE_ERROR);
        }
        productTypeMapper.updateById(productType);
    }

    @Override
    public void deleteProductType(Long id) {
        QueryWrapper<ProductType> queryWrapper = new QueryWrapper();
        queryWrapper.eq("parentId",id);
        List<ProductType> list = productTypeMapper.selectList(queryWrapper);
        if (!CollectionUtils.isEmpty(list)){
            throw new BusException(CodeEnum.DELETE_PRODUCTTYPE_ERROR);

        }
        productTypeMapper.deleteById(id);
    }

    @Override
    public ProductType findProductTypeById(Long id) {
        return productTypeMapper.selectById(id);
    }

    @Override
    public Page<ProductType> findProductTypeByPage(ProductType productType, int page, int size) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (productType != null && StringUtils.hasText(productType.getName())){
            queryWrapper.like("name",productType.getName());
        }
        if (productType != null && productType.getParentId() != null){
            queryWrapper.eq("parentId",productType.getParentId());
        }
        return productTypeMapper.selectPage(new Page<>(page,size),queryWrapper);
    }

    @Override
    public List<ProductType> findProductTypeByCondition(ProductType productType) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (productType != null && StringUtils.hasText(productType.getName())){
            queryWrapper.like("name",productType.getName());
        }
        if (productType != null && productType.getParentId() != null){
            queryWrapper.eq("parentId",productType.getParentId());
        }
        return productTypeMapper.selectList(queryWrapper);
    }
}
